package com.yourenbang.lingyun.transaction.util;

import com.yourenbang.lingyun.core.util.StringUtils;
import com.yourenbang.lingyun.core.util.encrypt.lkl.RSAUtil;
import com.yourenbang.lingyun.transaction.web.service.impl.pay.transaction.PayTransactionConfig;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.KeyFactory;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：lzq
 * @date ：Created in 2019/7/26 14:40
 * @description：正则表达式验证
 */
public class RegexUtil {


    /**
     * 验证中文
     */
    private static Pattern NUMBER_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");


    /**
     * 验证手机号
     */
    //public static final String REGEX_MOBILE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[0-9]))\\d{8}$";


    //public static final String REGEX_MOBILE = "^((12[0-9])|(13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";

    public static final String REGEX_MOBILE = "^((1[0-9][0-9]))\\d{8}$";
    /**
     * 判断字符串中是否包含中文
     *
     * @param str 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {

        Matcher m = NUMBER_PATTERN.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return Pattern.matches(REGEX_MOBILE, mobile);
    }




    public static void main(String[] args) {

        String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnF/kjstyIlCFjJe8PkZxhnv/tv26TjX8+mSOKpeUPU5hsXRwHMR8SapWaddLPQC15ead54b77HHmG3ZcPV0BoFXBOiwxN6es74+wla6EndLwd4IfYVvIfjyi6NPM4OmLidl0/NUoujbxVR9hFNuSCbLZW1h9/HP3Z0mFbLitpIwIDAQAB";
        String data = "VP5DoOfSxXY0XQEXYnhl6Wwy3agvFBpGIn3yKQG1oMSEhLa6CTYxX72jZrkFx2IsWhRQhQ26pme0YUOqFJvv3+awK/qaWlVD80L166BawMpRzpBG609YGSVpLh41RJCSOfHe4SOy0C0/Obs+V/RhzUQK1NYn7eeGj3km4XeuvT9GjEN4N/teDvOXBRIUfwlCoCtbd+/W6PTm/93C+TxPw1GHM8QDEi4TugFQ8lj6trowAebFe6Tlh3fX1IbNSR2g+2yP6KRhbiU5m92Wo8JrTrEOEbuC9+fFbvP7rY4Kb6CkSFcH3H93x7L4ZI8eiREWoUDPGnJQPSdxRCtrnXhLjg==";

        try {
            String decrypt =  RSAUtil.decrypt(pubKey, data);

            System.out.println("decrypt=" + decrypt);
        } catch ( Exception e) {
            e.printStackTrace();
        }

        String transTime = "2023-12-01 12:12:12";
        String base64 ="ewogICAgImNvbnRyYWN0SWQiOiI0NyIsCiAgICAiY3VzdG9tZXJUeXBlIjoiaUMiLAogICAgImN1c3RvbWVyTm8iOjM3MzIsCiAgICAiY3VzdG9tZXJOYW1lIjoiTCoiLAogICAgImN1c3RvbWVyQWRkcmVzcyI6IioiLAogICAgImV4dGVybmFsQ3VzdG9tZXJObyI6IjhoIiwKICAgICJwaG9uZU5vIjoibyoiLAogICAgImxpY2Vuc2VObyI6IlEqIiwKICAgICJsaWNlbnNlTmFtZSI6IjYqIiwKICAgICJsaWNlbnNlRXhwaXJlU3RhcnQiOiI4IiwKICAgICJsaWNlbnNlRXhwaXJlRW5kIjoiSyIsCiAgICAiaWRlbnRpdHlObyI6IjQqIiwKICAgICJpZGVudGl0eUV4cGlyZVN0YXJ0IjoiMyIsCiAgICAiaWRlbnRpdHlFeHBpcmVFbmQiOiJXaiIsCiAgICAibGVnYWxOYW1lIjoiVCoiLAogICAgImxlZ2FsUGhvbmVObyI6IioiLAogICAgImFnZW50Tm8iOjkxOTEsCiAgICAiYWdlbmN5Tm8iOjk5MjMsCiAgICAidGVybU5vcyI6ImxoIiwKICAgICJjb3JlVGVybUlkcyI6WwogICAgICAgIHsKICAgICAgICAgICAgImNvcmVUZXJtSWQiOjE4MTAsCiAgICAgICAgICAgICJ0ZXJtVHlwZUNvZGUiOjc1MTEsCiAgICAgICAgICAgICJ0ZXJtVHlwZU5hbWUiOiJGIgogICAgICAgIH0KICAgIF0sCiAgICAiYWN0aXZlTm8iOiJUcCIsCiAgICAib3BlblRpbWUiOiJHbCIsCiAgICAic3RhdHVzIjoiaCIsCiAgICAicmVtYXJrIjoiRSIsCiAgICAidXNlck5vIjo5MTkxCn0=";
        System.out.println("dddd=====" + StringUtils.left(StringUtils.replace(transTime,"-", ""), 6));


        System.out.println(PayTransactionConfig.getTransTableIndex("6a0b35e9a3bd11eead180242ac140008"  ));


    }


    /**
     * 公钥解密
     * @param pubKey Base64公钥
     * @param data Base64数据
     * @return 解密字符串
     * @throws Exception 解密异常
     */
    public static String decrypt(String pubKey, String data) throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] keyBytes = decoder.decode(pubKey.getBytes());
        byte[] dataBytes = decoder.decode(data.getBytes());
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes)));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] cache;
        for (int i = 0, offset = 0, length = dataBytes.length; length - offset > 0; i++, offset = i * 128) {
            if (length - offset > 128) {
                cache = cipher.doFinal(dataBytes, offset, 128);
            } else {
                cache = cipher.doFinal(dataBytes, offset, length - offset);
            }
            out.write(cache, 0, cache.length);
        }
        return out.toString();
    }

}
